package com.infonova.product.jtf.blame.svn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;


public class History {

    private String url;
    private String name;
    private String password;


    // String url = "http://svn.svnkit.com/repos/svnkit/trunk/doc";
    // String name = "anonymous";
    // String password = "anonymous";

    public History(String url, String name, String password) {
        this.url = url;
        this.name = name;
        this.password = password;
    }


    public List<Change> retrieveChanges(Date from, Date to) throws SVNException {
        DAVRepositoryFactory.setup();

        SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));

        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
        repository.setAuthenticationManager(authManager);

        long startRevision = ((from == null) ? 0 : repository.getDatedRevision(from));
        long endRevision = ((to == null) ? -1 : repository.getDatedRevision(to));

        Collection<SVNLogEntry> logEntries = repository.log(new String[] { "" }, null, startRevision, endRevision,
            true, true);

        List<Change> changes = new ArrayList<Change>();
        for (SVNLogEntry logEntry : logEntries) {
            Commit commit = new Commit();
            commit.setAuthor(logEntry.getAuthor());
            commit.setDate(logEntry.getDate());
            commit.setRevision(logEntry.getRevision());
            commit.setLog(logEntry.getMessage());

            for (Iterator changedPaths = logEntry.getChangedPaths().keySet().iterator(); changedPaths.hasNext();) {
                SVNLogEntryPath entryPath = (SVNLogEntryPath)logEntry.getChangedPaths().get(changedPaths.next());
                Change change = new Change();
                change.setCommit(commit);
                change.setType(entryPath.getType());
                change.setPartFilePath(entryPath.getPath().replaceFirst(".*trunk", ""));
                changes.add(change);
            }
        }
        return changes;
    }

}
// ---------------------------------------------
// revision: 1240
// author: alex
// date: Tue Aug 02 19:52:49 NOVST 2005
// log message: 0.9.0 is now trunk
//
// changed paths:
// A /trunk (from /branches/0.9.0 revision 1239)
// ---------------------------------------------
// revision: 1263
// author: sa
// date: Wed Aug 03 21:19:55 NOVST 2005
// log message: updated examples, javadoc files
//
// changed paths:
// M /trunk/doc/javadoc-files/javadoc.css
// M /trunk/doc/javadoc-files/overview.html
// M /trunk/doc/examples/src/org/tmatesoft/svn/examples/wc/StatusHandler.java
// ---------------------------------------------
// revision: 1287
// author: sa
// date: Mon Aug 08 18:27:37 NOVST 2005
// log message: modified javadoc.css, packages overview
//
// changed paths:
// M /trunk/doc/javadoc-files/javadoc.css
// M /trunk/doc/javadoc-files/overview.html
// D /trunk/doc/javadoc-files/subversion.png
// A /trunk/doc/javadoc-files/info.png
// ---------------------------------------------
// revision: 1294
// author: sa
// date: Tue Aug 09 02:46:29 NOVST 2005
// log message: corrected wc examples, modified javadoc files

